﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PromoServer.Library
{
    public class PromoLog
    {
        private const string logPath = "promolog.txt";
        private bool logOnlyErrors = false;

        public PromoLog()
        {
            try
            {
                // If log does not exist, create log file
                if (!File.Exists(logPath))
                {
                    using (StreamWriter swLog = File.CreateText(logPath))
                    {
                        swLog.WriteLine("***** PromoServer Log | Created: " + System.DateTime.Now.ToString() + " *****");
                        swLog.Close();
                    }
                }


                if (PromoServer.Library.Properties.Settings.Default.Log_Only_Errors)
                    logOnlyErrors = true;
                else
                    logOnlyErrors = false;
            }
            catch
            {
                // Swallow error
            }
        }

        public void Log(string functionName, string message, bool isError)
        {
            try
            {
                // Set message type
                string messageType;
                if (isError == true)
                    messageType = "Error";
                else
                    messageType = "Information";

                // If message is not an error and flag is set to only log errors, return
                if (!isError && logOnlyErrors)
                    return;

                StreamWriter swLog = new StreamWriter(logPath, true);
                swLog.WriteLine(System.DateTime.Now.ToString() + " | Type: [" + messageType + "] | Process: [" + functionName + "] | Message: [" + message + "]");
                swLog.Close();
            }
            catch
            {
                // Swallow error
            }
        }
    }
}
